package com.school.controller;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.school.entity.HljtSchoolimage;
import com.school.entity.web.BasicsInfo;
import com.school.service.impl.HljtBasicsInfoServiceImpl;
import com.school.service.impl.HljtSchoolServiceImpl;
import com.school.service.impl.HljtSettingServiceImpl;
import com.school.util.FileUploadUtil;
import com.school.util.MapperToJson;
import com.school.util.Message;
import com.school.util.PictureVideoUpload;
import com.school.util.RepUtil;
import com.school.util.WhetherNotToUpdate;

@Controller
@RequestMapping(value="BasicsInfo")
public class HljtBasicsInfonController {
	@Autowired
	HljtBasicsInfoServiceImpl oHljtBasicsInfoServiceImpl;
	@Autowired
	private HljtSchoolServiceImpl hljtSchoolServiceImpl;
	@Autowired
	private HljtSettingServiceImpl hljtSettingServiceImpl;
	
	
	/**
	 * 
	 * 查询基础信息
	 * @param rep
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/findBasicsInfo")
	@ResponseBody
	public String findBasicsInfo(HttpServletResponse rep,HttpServletRequest req){
		RepUtil.repPake(rep);
		String strPath=RepUtil.findAddress(req);
		return oHljtBasicsInfoServiceImpl.findBasicsInfo();
	}
	/**
	 * 添加基础信息
	 * @param imgFile
	 * @param rep
	 * @return
	 * @throws IOException  ,method = RequestMethod.POST
	 */
  	@RequestMapping("/saveBasicsInfo")
  	@ResponseBody
  	    public String upload( HttpSession session,HttpServletResponse rep,BasicsInfo oBasicsInfo,HttpServletRequest req) throws IOException {
  		
  		RepUtil.repPake(rep);
  		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
  		 MultipartFile file = null;
  		String imagePath = null;
  		if (isMultipart){ 
		    MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(req, MultipartHttpServletRequest.class);
		    
		    System.out.println("imagefile:"+multipartRequest.getFile("imgFile"));
		     file = multipartRequest.getFile("imgFile");
		     if(!file.isEmpty()) {
			     imagePath = PictureVideoUpload.savePictureVideo(file, WhetherNotToUpdate.getSaveDisk());
		    }else {
		    	imagePath=hljtSchoolServiceImpl.findSchool().getLogo();
		    }
  		}
		oBasicsInfo.setLogo(imagePath);
		return  oHljtBasicsInfoServiceImpl.saveBasicsInfo(oBasicsInfo);
  	}
  
  
}
